package com.huayi.test.springbootcomm.service.impl;

import com.huayi.test.springbootcomm.component.ApplicationContextProvider;
import com.huayi.test.springbootcomm.constant.ErrCodeInfo;
import com.huayi.test.springbootcomm.exception.BizException;
import com.huayi.test.springbootcomm.service.IserviceContext;
import com.huayi.test.springbootcomm.service.IserviceFactory;
import com.huayi.test.springbootcomm.trans.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.util.Map;

public class ServiceFactory implements IserviceFactory {

    /**
     * 服务列表
     */
    private  static Map<String,String> serviceMap;

    public Map<String, String> getServiceMap() {
        return serviceMap;
    }

    public void setServiceMap(Map<String, String> serviceMap) {
        this.serviceMap = serviceMap;
    }

    @Autowired
    private ApplicationContextProvider applicationContextProvider;



    @Override
    public IService getService(String tranCode) {
        String innerTransCode = serviceMap.get(tranCode);
        ApplicationContext applicationContext = applicationContextProvider.getApplicationContext();
        IService serviceBean = (IService) applicationContext.getBean(innerTransCode);
        if (serviceBean==null){
            throw new BizException(ErrCodeInfo.TRANS_CODE_NOT_FOUND.getErrCode(),ErrCodeInfo.TRANS_CODE_NOT_FOUND.getErrMsg());
        }
        return serviceBean ;
    }
}
